package com.sunrate.investingquote.service.router.comparator.test.compare.v1;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.sunrate.investingquote.bean.router.ChannelPriorityConfig;
import com.sunrate.investingquote.bean.router.ChannelRouter;
import com.sunrate.investingquote.dto.router.comparator.RouterSortContext;
import com.sunrate.investingquote.util.ClassUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yyh
 * @description:
 * @date 2023/07/04/ 15:31
 */
public class ComparatorChain {
    ComparatorCreator chainHeader;
    ChainParam chainParam;

    public ComparatorChain(ChainParam chainParam) {
        this.chainParam = chainParam;
        createChain();
    }

    public void createComparator() {
        chainHeader.createAndInvokeNext(this.chainParam);
    }

    private void createChain() {
        List<ComparatorCreator> list = ClassUtils.getSonInstance(ComparatorCreator.class).stream()
                .peek(o -> o.setOrder(chainParam))
                .filter(o -> o.support(chainParam))
                .sorted(Comparator.comparing(ComparatorCreator::getOrder).reversed())
                .collect(Collectors.toList());
        ComparatorCreator headerTmp = null;
        for (ComparatorCreator creator : list) {
            if (headerTmp == null) {
                chainHeader = creator;
                headerTmp = creator;
            } else {
                headerTmp.setNext(creator);
                headerTmp = creator;
            }
        }
    }


    public static void main(String[] args) {
        ChainParam chainParam = new ChainParam();
        List<ChannelPriorityConfig> configs = Lists.newArrayList();
        ChannelPriorityConfig config = new ChannelPriorityConfig();
        config.setCode("fee_rate");
        config.setSortNum(99);

        ChannelPriorityConfig configaaa = new ChannelPriorityConfig();
        configaaa.setCode("cost_time");
        configaaa.setSortNum(98);

        configs.add(config);
        configs.add(configaaa);

        chainParam.setConfigs(configs);
        ComparatorChain chain = new ComparatorChain(chainParam);
        chain.createComparator();


        Comparator<RouterSortContext<ChannelRouter>> comparator = chainParam.getComparator();
        List<RouterSortContext<ChannelRouter>> list = new ArrayList<>();
        RouterSortContext<ChannelRouter> a = new RouterSortContext<>();
        a.setFeeRate(new BigDecimal("44"));
        a.setCostTime(44);
        a.setDefaultPriority(100);

        RouterSortContext<ChannelRouter> b = new RouterSortContext<>();
        b.setFeeRate(new BigDecimal("44"));
        b.setCostTime(44);
        b.setDefaultPriority(50);

        RouterSortContext<ChannelRouter> c = new RouterSortContext<>();
        c.setFeeRate(new BigDecimal("44"));
        c.setCostTime(44);
        c.setDefaultPriority(300);

        list.add(a);
        list.add(b);
        list.add(c);
        list.sort(comparator);
        list.forEach(o -> System.out.println(JSON.toJSONString(o)));
//        System.out.println(JSON.toJSONString(chainParam));
    }

}
